Skip to content

linters: add forbiddenmarkers and nonullable linters #111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

everettraven
Copy link
Contributor

Adds a disabled by default forbiddenmarkers linter that has configuration options to specify marker identifiers that should never exist on a type/field. Suggests fixes to remove the forbidden markers.

Adds an enabled by default nonullable linter that wraps the forbiddenmarkers linter and configures the nullable marker to be forbidden.

Fixes #100

Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: everettraven
Once this PR has been reviewed and has the lgtm label, please assign jpbetz for approval. For more information see the Code Review Process.

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot requested a review from JoelSpeed June 26, 2025 13:48
@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jun 26, 2025
@k8s-ci-robot k8s-ci-robot requested a review from jpbetz June 26, 2025 13:48
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Jun 26, 2025
Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@everettraven
Copy link
Contributor Author

I'll need to add the linter documentation to the new location as well :P

/hold

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Jun 26, 2025
const name = "forbiddenmarkers"

type analyzer struct {
forbiddenMarkers []string
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if I wanted to forbid a marker only if it had a certain parameter? Or a certain value?

E.g. I want to prevent someone from using a particular kubebuilder:validation:Format, would this allow me to do that?

Or I wanted to say, you can't use the optionalOldSelf property on an XValidation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good questions. This would not allow that for now. I think those are reasonable use cases and make sense to consider.

I can add some configuration options to allow for this type of customization.

Comment on lines 33 to 34
analyzer.Name = name
analyzer.Doc = "Check that nullable marker is not present on any types or fields."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create a constructor on the forbidden markers side that allows you to pass options for Name and Doc, rather than overriding them here? Then this just becomes

return NewAnalyzerWithOpts(forbidden.Opts{
  Name: ...
  Doc: ...
  Markers: []string{...}
})

Or even, you could just call that inside the initializer.

Comment on lines +18 to +21
* The `nonullable` linter ensures that types and fields do not have the `nullable` marker.
*
* Fixes are suggested to remove the `nullable` marker.
*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the asterisks at the beginning of each line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copied format from another linter doc.go - but also I think my editor does this by default. I can strip the asterisks at the beginning of each line if necessary

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter to me, but normally with /* */ syntax I didn't think it was needed

Comment on lines +23 to +45
// Initializer returns the AnalyzerInitializer for this
// Analyzer so that it can be added to the registry.
func Initializer() initializer {
return initializer{}
}

// intializer implements the AnalyzerInitializer interface.
type initializer struct{}

// Name returns the name of the Analyzer.
func (initializer) Name() string {
return name
}

// Init returns the intialized Analyzer.
func (initializer) Init(_ config.LintersConfig) (*analysis.Analyzer, error) {
return newAnalyzer(), nil
}

// Default determines whether this Analyzer is on by default, or not.
func (initializer) Default() bool {
return true
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm starting to wonder if we should create a util to generate the initializer from a func 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that idea, but to keep the scope of this PR tightened to these two linters I'd suggest that be a follow up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now implemented, PR will need a rebase

Signed-off-by: Bryce Palmer <bpalmer@redhat.com>
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jun 27, 2025
@everettraven
Copy link
Contributor Author

Working on allowing configuration of forbidden attributes and values.

/hold

@k8s-ci-robot
Copy link
Contributor

PR needs rebase.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Jul 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

New Linter: nonullable
3 participants